import numpy as np
import soundfile as sf

def extract_watermark(audio_path, watermark_length):
    audio, _ = sf.read(audio_path)
    if audio.ndim > 1:
        audio = audio.mean(axis=1)

    audio_int = (audio * (2 ** 15)).astype(np.int16)
    watermark_bin = ''.join([str(audio_int[i] & 1) for i in range(watermark_length)])

    # 二进制转字符串
    watermark = ''
    for i in range(0, watermark_length, 8):
        byte = watermark_bin[i:i + 8]
        watermark += chr(int(byte, 2))

    return watermark


# 示例：提取8字节水印（如日期长度）
print(extract_watermark("output.wav", 128))  # 输出：2025-03-30